/**
 * 
 */
package com.caisejingyu.whales.home.devices.printer;

import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author GaoGao
 * @date 2015-5-18
 * @description 
 */
class SharePreferenceBooster {
	
	private static SharePreferenceBooster mSpBooster;
	private ExecutorService mExecutor;
	
	private SharePreferenceBooster() {
		mExecutor = Executors.newSingleThreadExecutor();
	}
	
	public static SharePreferenceBooster instance() {
		if (mSpBooster == null) {
			mSpBooster = new SharePreferenceBooster();
		}
		return mSpBooster;
	}
	
	public void put(SharedPreferences sharePreference, String key, String string) {
		synchronized (this) {
			Editor edit = sharePreference.edit();
			edit.putString(key, string);
			edit.commit();
			edit.clear();
		}
	}
		
	public void putAsync(final SharedPreferences sharePreference, final String key, final String value) {
		mExecutor.execute(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				put(sharePreference, key, value);
			}
		});

	}

	public String get(SharedPreferences sharePreference, String key, String defaultValue) {
		synchronized (this) {
			String value = sharePreference.getString(key, defaultValue);
			return value;
		}
	}
	
	public void putBoolean(SharedPreferences sharePreference, String key, boolean value) {
		synchronized (this) {
			Editor edit = sharePreference.edit();
			edit.putBoolean(key, value);
			edit.commit();
			edit.clear();
		}
	}
	
	public boolean getBoolean(SharedPreferences sharePreference, String key, boolean defaultValue) {
		synchronized (this) {
			boolean value = sharePreference.getBoolean(key, defaultValue);
			return value;
		}
	}

	public void putInt(SharedPreferences sharePreference, String key, int value) {
		synchronized (this) {
			Editor edit = sharePreference.edit();
			edit.putInt(key, value);
			edit.commit();
			edit.clear();
		}
	}
	
	public int getInt(SharedPreferences sharePreference, String key, int defaultValue) {
		synchronized (this) {
			int value = sharePreference.getInt(key, defaultValue);
			return value;
		}
	}
	
}
